package com.youlu.campus.web.point;

import com.youlu.campus.entity.PointPlatformTaskConfigQueryVO;
import com.youlu.campus.entity.PointPlatformTaskConfigVO;
import com.youlu.campus.entity.UserInfoExtend;
import com.youlu.campus.entity.UserPointTaskInfo;
import com.youlu.campus.entity.VO.req.PlatformTaskQueryVO;
import com.youlu.campus.entity.enums.TaskBizType;
import com.youlu.campus.service.platform.PlatformTaskService;
import com.youlu.campus.service.platform.PointPlatformTaskConfigService;
import com.youlu.campus.service.point.UserPointTaskInfoService;
import com.youlu.campus.service.visitor.UserAppSubWechatTemplateMsgService;
import com.youlu.campus.service.visitor.UserInfoExtendService;
import com.youlu.campus.service.visitor.UserPointDailySignInService;
import com.youlu.campus.service.visitor.UserPointsCourseStudyService;
import com.youlu.campus.web.common.base.RequestContext;
import com.yuelin.infrastructure.quantum.common.QResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@RestController
public class UserPointTaskController {
    @Autowired
    private UserPointTaskInfoService userPointTaskInfoService;
    @Autowired
    private UserPointDailySignInService userPointDailySignInService;
    @Autowired
    private UserAppSubWechatTemplateMsgService userAppSubWechatTemplateMsgService;
    @Autowired
    private UserPointsCourseStudyService userPointsCourseStudyService;
    @Autowired
    private PlatformTaskService platformTaskService;
    @Autowired
    private PointPlatformTaskConfigService pointPlatformTaskConfigService;
    @Autowired
    private UserInfoExtendService userInfoExtendService;

    /**
     * 积分平台任务
     *
     * @return
     */
    @GetMapping("/mini-app/point/tasks")
    public QResult getTasks() {
        List<UserPointTaskInfo> tasks = userPointTaskInfoService.find(RequestContext.getUserId(),
                RequestContext.getAppId(), RequestContext.getCurPlatform());
        List<TaskBizType> types = new ArrayList<>();
        types.add(TaskBizType.JOIN_ACTIVITY);
        types.add(TaskBizType.INVITE);
        types.add(TaskBizType.PERFECT_INFO);
        types.add(TaskBizType.VIDEO);
        for (TaskBizType type : types) {
            boolean in = false;
            for (UserPointTaskInfo userPointTaskInfo : tasks) {
                if (type.equals(userPointTaskInfo.getTaskBizType())) {
                    in = false;
                    break;
                }
            }
            if (!in) {
                UserPointTaskInfo u = new UserPointTaskInfo();
                u.setUserId(RequestContext.getUserId());
                u.setAppId(RequestContext.getAppId());
                u.setTaskBizType(type);
                u.setStatus(0);
                tasks.add(u);
            }
        }
        boolean signed = userPointDailySignInService.signIned(RequestContext.getUserId(), new Date());
        UserPointTaskInfo u = new UserPointTaskInfo();
        u.setUserId(RequestContext.getUserId());
        u.setAppId(RequestContext.getAppId());
        u.setTaskBizType(TaskBizType.SIGN_UP);
        if(signed){
            u.setStatus(1);
        }else {
            u.setStatus(0);
        }
        tasks.add(u);
        return QResult.success(tasks);
    }

    /**
     * 微信小程序积分平台任务
     *
     * @return
     */
    @GetMapping("/mini-app/point/my-tasks")
    public QResult getAppTasks() {
        List<UserPointTaskInfo> tasks = userPointTaskInfoService.find(RequestContext.getUserId(),
                RequestContext.getAppId(), RequestContext.getCurPlatform());
        List<TaskBizType> types = new ArrayList<>();
        types.add(TaskBizType.COURSE_STUDY);
        types.add(TaskBizType.PERFECT_INFO);
        types.add(TaskBizType.VIDEO);
        for (TaskBizType type : types) {
            UserPointTaskInfo u = new UserPointTaskInfo();
            u.setUserId(RequestContext.getUserId());
            u.setAppId(RequestContext.getAppId());
            u.setTaskBizType(type);
            u.setStatus(0);
            tasks.add(u);
        }

        boolean Subscribed = userAppSubWechatTemplateMsgService.subscribed(RequestContext.getUserId(),RequestContext.getCurPlatform(),RequestContext.getAppId());
        UserPointTaskInfo sub = new UserPointTaskInfo();
        sub.setUserId(RequestContext.getUserId());
        sub.setAppId(RequestContext.getAppId());
        sub.setTaskBizType(TaskBizType.SUBSCRIPTION);
        if(Subscribed){
            sub.setStatus(1);
        }else {
            sub.setStatus(0);
        }
        tasks.add(sub);

        boolean signed = userPointDailySignInService.signIned(RequestContext.getUserId(), new Date());
        UserPointTaskInfo u = new UserPointTaskInfo();
        u.setUserId(RequestContext.getUserId());
        u.setAppId(RequestContext.getAppId());
        u.setTaskBizType(TaskBizType.SIGN_UP);
        if(signed){
            u.setStatus(1);
        }else {
            u.setStatus(0);
        }
        tasks.add(u);
        return QResult.success(tasks);
    }

    /**
     * 微信小程序积分平台任务
     *
     * @return
     */
    @GetMapping("/mini-app/point/platform-tasks")
    public QResult getPlatformTasks() {
        List<UserPointTaskInfo> tasks = userPointTaskInfoService.find(RequestContext.getUserId(),
                RequestContext.getAppId(), RequestContext.getCurPlatform());
        PointPlatformTaskConfigQueryVO pointPlatformTaskConfigQueryVO = new PointPlatformTaskConfigQueryVO();
        pointPlatformTaskConfigQueryVO.setPlatform(RequestContext.getCurPlatform());
        pointPlatformTaskConfigQueryVO.setAppId(RequestContext.getAppId());
        pointPlatformTaskConfigQueryVO.setStatus(1);
        Page<PointPlatformTaskConfigVO> platformTaskInfos = pointPlatformTaskConfigService.list(pointPlatformTaskConfigQueryVO);
        platformTaskInfos.stream().map(platformTaskInfo->{
            switch (platformTaskInfo.getTaskBizType()){
                case SUBSCRIPTION:
                    boolean Subscribed = userAppSubWechatTemplateMsgService.subscribed(RequestContext.getUserId(),RequestContext.getCurPlatform(),RequestContext.getAppId());
                    platformTaskInfo.setStatus(Subscribed?1:0);
                    break;
                case SIGN_UP:
                    boolean signed = userPointDailySignInService.signIned(RequestContext.getUserId(), new Date());
                    platformTaskInfo.setStatus(signed?1:0);
                    break;
                case PERFECT_INFO:
                    UserInfoExtend userInfoExtend = userInfoExtendService.findByUserIdAndAppId(RequestContext.getCurUser());
                    boolean perfected;
                    if(Objects.isNull(userInfoExtend)){
                        perfected = false;
                    }else {
                        userInfoExtend.checkUserInfoComplete();
                        perfected = userInfoExtend.getUserInfoComplete();
                    }
                    platformTaskInfo.setStatus(perfected?1:0);
                    break;
                default:
                    platformTaskInfo.setStatus(0);
                    break;
            }
            return platformTaskInfo;
        }).collect(Collectors.toList());
        return QResult.success(platformTaskInfos);
    }

    /**
     * 微信小程序积分平台任务
     *
     * @return
     */
    @PostMapping("/mini-app/point/task-list")
    public QResult getAppTaskList(@RequestBody PlatformTaskQueryVO req) {        ;
        return QResult.success(platformTaskService.getTaskList(req));
    }

    /**
     * 微信小程序积分平台课程学习任务
     *
     * @return
     */
    @GetMapping("/mini-app/point/go-study")
    public QResult studyTask() {
        return QResult.success(userPointsCourseStudyService.startStudy(RequestContext.getUserId(),RequestContext.getCurPlatform(),RequestContext.getAppId()));
    }
}
